import type { RequestTask } from './utils'

export interface HttpConfig {
  baseURL?: string
  timeout?: number
}

export type HttpRequestMethod =
  | 'get'
  | 'delete'
  | 'head'
  | 'options'
  | 'post'
  | 'put'
  | 'patch'

export interface HttpProgress {
  loaded: number
  total?: number
}

export type HttpDataTransformer = (
  data: any,
  headers?: Record<string, any>
) => any

export interface HttpRequestConfig {
  url?: string
  method?: HttpRequestMethod | string
  data?: any
  headers?: Record<string, any>
  timeout?: number
  transformRequest?: HttpDataTransformer | HttpDataTransformer[]
  transformResponse?: HttpDataTransformer | HttpDataTransformer[]
  params?: any
  onUploadProgress?: (p: HttpProgress) => void
  onDownloadProgress?: (p: HttpProgress) => void
  responseType?:
    | 'arraybuffer'
    | 'blob'
    | 'document'
    | 'json'
    | 'text'
    | 'stream'

  readonly _initiator?: any
}

export interface HttpResponse<
  T = any,
  CT extends HttpRequestConfig = HttpRequestConfig
> {
  data: T
  status: number
  statusText: string
  headers: Record<string, any>
  config: CT
}

export interface Http<
  T = any,
  CT extends HttpRequestConfig = HttpRequestConfig
> {
  <DT = T>(config: CT): RequestTask<DT>

  head: <DT = T>(url: string, config?: CT) => RequestTask<DT>
  get: <DT = T>(url: string, config?: CT) => RequestTask<DT>
  post: <DT = T>(url: string, data?: any, config?: CT) => RequestTask<DT>
  put: <DT = T>(url: string, data?: any, config?: CT) => RequestTask<DT>
  delete: <DT = T>(url: string, config?: CT) => RequestTask<DT>
}
